package com.yuanqi.contoller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuanqi.entity.WareWarehouse;
import com.yuanqi.service.WareWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Autowired
    private WareWarehouseService warehouseService;

    /**
     * 分页获取仓库列表
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String manager
    ) {
        try {
            IPage<WareWarehouse> pageResult = warehouseService.listWarehouses(page, size, name, manager);

            Map<String, Object> result = new HashMap<>();
            result.put("code", 0);
            result.put("message", "查询成功");
            result.put("records", pageResult.getRecords());
            result.put("total", pageResult.getTotal());
            result.put("pages", pageResult.getPages());
            result.put("current", pageResult.getCurrent());
            result.put("size", pageResult.getSize());

            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("code", 500);
            errorResult.put("message", "查询仓库列表失败: " + e.getMessage());
            errorResult.put("records", new ArrayList<>());
            errorResult.put("total", 0);
            errorResult.put("pages", 0);
            errorResult.put("current", page);
            errorResult.put("size", size);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
        }
    }



    /**
     * 根据ID获取仓库
     */
    @GetMapping("/{id}")
    public ResponseEntity<WareWarehouse> getById(@PathVariable Long id) {
        WareWarehouse wareWarehouse = warehouseService.getWarehouseById(id);
        if (wareWarehouse == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(wareWarehouse);
    }

    /**
     * 新增仓库
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody WareWarehouse wareWarehouse) {
        boolean success = warehouseService.saveWarehouse(wareWarehouse);
        if (success) {
            return new ResponseEntity<>(HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 更新仓库
     */
    @PutMapping("/{id}")
    public ResponseEntity<Void> update(@PathVariable Long id, @RequestBody WareWarehouse warehouse) {
        warehouse.setId(id);
        boolean success = warehouseService.updateWarehouse(warehouse);
        if (success) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除仓库
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        boolean success = warehouseService.deleteWarehouse(id);
        if (success) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}
